package com.mornd.gulimall.ware.controller;

import com.mornd.gulimall.common.core.result.AjaxResult;
import com.mornd.gulimall.common.core.validation.UpdateGroup;
import com.mornd.gulimall.ware.entity.PurchaseDetail;
import com.mornd.gulimall.ware.service.IPurchaseDetailService;
import org.hibernate.validator.constraints.Range;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

import static com.mornd.gulimall.common.core.result.AjaxResult.success;

/**
 * <p>
 * 采购需求 前端控制器
 * </p>
 *
 * @author mornd
 * @since 2023-07-29
 */

@Validated
@RestController
@RequestMapping("/ware/purchaseDetail")
public class PurchaseDetailController {
    @Resource
    private IPurchaseDetailService purchaseDetailService;

    @GetMapping
    public AjaxResult list(PurchaseDetail detail) {
        return success(purchaseDetailService.list(detail));
    }

    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return success(purchaseDetailService.getById(id));
    }

    @PutMapping
    public AjaxResult save(@RequestBody @Validated PurchaseDetail detail) {
        purchaseDetailService.save(detail);
        return success();
    }

    @PostMapping
    public AjaxResult update(@RequestBody @Validated(UpdateGroup.class) PurchaseDetail detail) {
        purchaseDetailService.updateById(detail);
        return success();
    }

    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        purchaseDetailService.removeByIds(Arrays.asList(ids));
        return success();
    }

    /**
     * 合并整单
     *
     * @param purchaseId
     * @param detailIds
     * @return
     */
    @PostMapping("/merge/{purchaseId}/{detailIds}")
    public AjaxResult merge(@PathVariable("purchaseId") @NotNull @Range Long purchaseId,
                            @PathVariable("detailIds") @NotNull @NotEmpty Long[] detailIds) {
        // todo 校验采购需求的状态
        Long id = purchaseDetailService.merge(purchaseId, detailIds);
        return success(id);
    }

}
